using System.Drawing;
using System.Windows.Forms;
using CanvasControl.Interface;
using CanvasControl.Model;
using CanvasControl.View;

namespace GraphDrawing.Graph.Behaviors
{
    public class DebugBehavior : ICanvasBehavior, IPainter, IInputListener
    {
        readonly Keys debugSwitch;
        bool enabled;
        CanvasModel canvas;

        public DebugBehavior(Keys debugSwitch, bool enabled)
        {
            this.debugSwitch = debugSwitch;
            this.enabled = enabled;
        }

        public void Install(ICanvasView view, CanvasModel canvas)
        {
            this.canvas = canvas;
            canvas.AddPainter(PaintStages.POSTPROCESSING, this);
            canvas.Subscribe(this);
        }

        public void Uninstall(CanvasModel canvas1)
        {
            canvas.RemovePainter(this);
            canvas.Unsubscribe(this);
        }

        void IInputListener.OnMouseUp(Point p)
        {
        }

        void IInputListener.OnMouseMove(Point p)
        {
        }

        void IInputListener.OnMouseDown(InputState input)
        {
        }

        public void OnKeyDown(Keys key)
        {
            if (key == debugSwitch)
                enabled = !enabled;
        }

        void IPainter.Draw(DrawingContext dc)
        {
            if (!enabled)
                return;
            var g = dc.Graphics;
            var viewPort = canvas.ViewPort;
            using (var font = new Font("Courier New", 9f))
            {
                g.DrawString("Scale: " + viewPort.Scale, font, Brushes.Black, 0, 0);
                g.DrawString(string.Format("ViewPortPosition: {0}", viewPort.Position), font, Brushes.Black, 0, 11);
                g.DrawString(string.Format("ViewPortSize: {0}", viewPort.Size), font, Brushes.Black, 0, 22);
                g.DrawString(string.Format("DragToleranceSize: {0}", SystemMetrics.GetDragToleranceSize()), font, Brushes.Blue, 0, 33);
            }
        }
    }
}